package com.mskj.p3.controller;


import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.mskj.p3.model.Blog;
import com.mskj.p3.model.Help;
import com.mskj.p3.model.Hobby;
import com.mskj.p3.model.PageBean;
import com.mskj.p3.model.User;
import com.mskj.p3.service.BlogService;
import com.mskj.p3.service.HelpService;
import com.mskj.p3.service.HobbyService;
import com.mskj.p3.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userservice;
	@Autowired
	private HelpService helpservice;
	@Autowired
	private HobbyService hobbyservice;
	@Autowired
	private BlogService blogservice;
	
	@RequestMapping("login.do")
	public String login(User user,HttpSession session){
		User user1=userservice.login1(user);
		if(userservice.login(user)){
			session.setAttribute("u_id",user1.getUid());
			return "/logined/welcome";
		}
		return "redirect:../login.jsp";
	}
	
	@RequestMapping("regist.do")
	public String regist(User user,Hobby h) {
		if(userservice.reigist(user,h)){
			return "redirect:../login.jsp";
		}
		return "redirect:../login.jsp";
	}

	
	@RequestMapping("alter.do") //修改个人信息
	public ModelAndView  alter(User user,Hobby h,Model model,HttpSession session){
	  System.out.println("名字："+user.getUname());
	  System.out.println("密码："+user.getUpsd());
	  user.setUid((String)session.getAttribute("u_id"));
	  System.out.println("set后名字："+user.getUname());
	  System.out.println("set后密码："+user.getUpsd());
	  System.out.println("controller层hinfo："+h.getHinfo());
	  if(h.getHinfo()!=null){
		  hobbyservice.addhobbys(user, h);
	  }
	  userservice.alter(user);
	  hobbyservice.deleteHobby(h, user); 
		String url = "redirect:/user/displayuser.do";
		return new ModelAndView(url);
	}
	
	@RequestMapping("displayuser.do") //修改个人信息
	public ModelAndView  displayuser(User user,Model model,HttpSession session){
		user.setUid((String)session.getAttribute("u_id"));
		model.addAttribute("displayuser",userservice.displayUser(user));
	    model.addAttribute("hobbylist",hobbyservice.queryHobby(user));
		return new ModelAndView("logined/alter");
	}
	
	@RequestMapping("deletehobby.do") //删除爱好
	public ModelAndView  deletehobby(User user,HttpSession session,Hobby h,Model model){
		user.setUid((String)session.getAttribute("u_id"));
	    hobbyservice.deleteHobby(h, user); 
		String url = "redirect:/user/displayuser.do";
		return new ModelAndView(url);
	}

	
	@RequestMapping("myblog.do")
	public ModelAndView myquery(HttpSession hs,PageBean pb,Model m){
		User u = new User((String)hs.getAttribute("u_id"));
		m.addAttribute("myblog",blogservice.queryMyBlogSet(u, pb));
		return new ModelAndView("logined/myblog");
	}
	
	@RequestMapping("myhelp.do")
	public ModelAndView myhelp(HttpSession hs,PageBean pb,Model m){
		User u = new User((String)hs.getAttribute("u_id"));
		m.addAttribute("myhelp",helpservice.queryMyHelpSet(u, pb));
		return new ModelAndView("logined/myhelp");
	}
	
	@RequestMapping("mymsg.do")
	public String mymsg(User user){
		return "mymsg";
	}
	
	@RequestMapping("delete.do")
	public ModelAndView delete(Blog b,Help h,HttpSession hs){
		User u = new User((String)hs.getAttribute("u_id"));
		if(b.getBid()!=null){
			blogservice.deleteBlog(u, b);
		    return new ModelAndView("redirect:/user/blog.do");
		}
		helpservice.deleteHelp(u, h);
		return new ModelAndView("redirect:/user/help.do");
		
		
	}
	
	@RequestMapping("blog.do")
	public ModelAndView blog(HttpSession hs,PageBean pb,Model model){
		User u = new User((String)hs.getAttribute("u_id"));
		System.out.println("博文板块uid:"+u.getUid());
		model.addAttribute("blogsetlist",blogservice.queryBlogSet(u, pb));
		model.addAttribute("hobbylist",hobbyservice.queryHobby(u));
		return new ModelAndView("logined/postblog");
	}
	
	@RequestMapping("help.do")
	public ModelAndView help(HttpSession hs,PageBean pb,Model model){
		User u = new User((String)hs.getAttribute("u_id"));
		model.addAttribute("helpsetlist",helpservice.queryHelpSet(u, pb));
		model.addAttribute("hobbylist",hobbyservice.queryHobby(u));
		return new ModelAndView("logined/posthelp");
	}
	
	@RequestMapping("post.do")
	public ModelAndView post(HttpSession hs,Hobby hobby,Blog b,Help help){
		User u = new User((String)hs.getAttribute("u_id"));
		System.out.println("发表博文uid:"+u.getUid());
		if(b.getBinfo()!=null){
			blogservice.postBlog(u,b,hobby);
			return new ModelAndView("redirect:/user/blog.do");
		}
		helpservice.postHelp(u,help,hobby);
		return new ModelAndView("redirect:/user/help.do");
		
	}
	
	@RequestMapping("changePhoto.do")
    public ModelAndView uploadPhoto(MultipartFile filePhoto,HttpSession httpSession,HttpServletRequest request){
		User user = new User((String)httpSession.getAttribute("u_id"));
        ServletContext context=httpSession.getServletContext();
        //上传路径 需注意，在tomcat中配置虚拟路径或者在其目录下的conf中的server.xml中的<Host>节点下配置
// <Context path="/项目名/虚拟的路径如pic，任何字符都可以" docBase="D:0upload0users0imgs"></Context>
       //当你在项目中使用/pic时，实际使用的是D:0upload0users0imgs
                String filePath="D:/upload/users/imgs/";    
                //获取图片原始名称
                String originalFilename=filePhoto.getOriginalFilename();
                //图片扩展名
                String types=originalFilename.substring(originalFilename.lastIndexOf(".")+1).toLowerCase();
        
            try {
                System.out.println(types);
                //以用户id加图片扩展名给图片命名
                String newFileName=user.getUid()+originalFilename.substring(originalFilename.lastIndexOf("."));
                File file=new File(filePath+newFileName);
                //上传
                filePhoto.transferTo(file);
                //以80*80大小改变图片，此处使用thumbnailator-0.4.2.jar改变图片大小
//                Thumbnails.of(file).size(80, 80).keepAspectRatio(false).toFile(file);
//                userBiz.updatePhoto(user,newFileName);
                context.setAttribute("photo","photo");
                } catch (Exception e) {
                    e.printStackTrace();
                }            
        return new ModelAndView("/user/success"); 
    }
	@RequestMapping("cblog.do") //评论博文
	public String cblog(User user,Blog b){
		return "blog";
	}
	
	@RequestMapping("chelp.do") //评论救急
	public String chelp(User user,Help h){
		return "help";
	}
}
